<template>
  <!-- 商品列表每一项的子组件 -->
  <view>
      <!-- 商品列表每一项 -->
        <view class="goods-item">
          <!-- 商品左侧图片 -->
            <view class="goods-item-left">
             <!-- <radio checked color="#C00000"></radio> -->
             <radio :checked='goods.goods_state' color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
              <image :src="goods.goods_small_logo||defaultPic" class="goods-pic"></image>
            </view>
           <!-- /商品左侧图片 -->
           <!-- 商品右侧区域 -->
            <view class="goods-item-right">
              <!-- 商品名称区域 -->
              <view class="goods-name">{{goods.goods_name}}</view>
               <!-- /商品名称区域 -->
               <!-- 商品价格区域 -->
               <view class="goods-info-box">
                 <view class="goods-price">¥{{goods.goods_price|toFixed}}</view>
                 <!-- //商品数量 -->
                 <uni-number-box min="1" 
                 :value="goods.goods_count" 
                 v-if="showNum" @change="numChangeHandler"></uni-number-box>
               </view>
               <!-- /商品价格区域 -->
            </view>
           <!-- /商品右侧区域 --> 
        </view>
       <!-- /商品列表每一项 -->
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    // 过滤器
    filters:{
        //商品价格过滤器  保留小数点两位小数
        toFixed(num){
          //将价格是字符串转换为数字形式 然后.toFixed(2)保留两位小数
          return Number(num).toFixed(2)
        }
    },
    props:{
      goods:{
        type:Object,
        default:{},
      },
      //定义是否展示选中商品图标
      showRadio:{
        type:Boolean,
        default:false
      },
      //定义是否展示商品数量选择框
      showNum:{
        type:Boolean,
        default:false
      }
    },
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods: {
      //商品状态发生了变化
      radioClickHandler(){
       this.$emit('radio-change',{
         //商品的id
         goods_id:this.goods.goods_id,
         //商品的状态
         goods_state:!this.goods.goods_state
       })
      },
      //商品数量发生了变化
      numChangeHandler(val){
        this.$emit('num-change',{
         //商品的id
         goods_id:this.goods.goods_id,
         //商品的数量
         //+号是将字符串转换为数字
         goods_count:+val
        })
      }
    }
  }
</script>

<style lang="scss">
 .goods-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;
    .goods-item-left{
      display: flex;
      align-items: center;
      margin-right: 5px;
      .goods-pic{
        width: 100px;
        height: 100px;
        // 去除图片与图片之间下间隙
        display: block;
      }
    }
    .goods-item-right{
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;
      .goods-name{
          font-size: 13px;
      }
      .goods-info-box{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .goods-price{
           font-size: 16px;
           color: #c00000;
        }
      }
    }
  }
</style>
